import request from '@/utils/request'
import axios from 'axios'
import { getToken as getAuthToken } from '@/utils/auth'

export function getToken() {
  return request({
    url: '/file/upload/token',
    method: 'get'
  })
}

export function getTokenForPriv() {
  return request({
    url: '/file/upload/token',
    method: 'get',
    params: { bucket: 'yanxioabai-priv', domain: 'http://cdn.yanxiaobai.com' }
  })
}

export function upload(formData) {
  const config = {
    headers: { 'Content-Type': 'multipart/form-data' }
  }
  formData.append('chunk', '0')
  return axios.post('https://upload.qbox.me', formData, config)
}

export function attchUpload(formData, type) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  return axios.post(process.env.VUE_APP_BASE_API + '/file/upload?type=' + type, formData, config)
}

export function tcAttchUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  return axios.post(process.env.VUE_APP_BASE_API + '/tc/file/upload?type=' + type + '&relId=' + relId, formData, config)
}

export function avatarUpload(formData, type) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  return axios.post(process.env.VUE_APP_BASE_API + '/file/uploadAvatar', formData, config)
}

export function livecodeAttchUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/livecode/file/upload?type=' + type + '&relId=' + relId, formData, config)
}

export function subjectAttchUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/subject/file/upload?type=' + type + '&relId=' + relId, formData, config)
}

export function postAttchUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/post/file/upload?type=post&relId=' + relId, formData, config)
}

export function schoolAttchUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/school/file/upload?type=school&relId=' + relId, formData, config)
}

export function kySubjectAttchUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/ky/subject/file/upload?type=' + type + '&relId=' + relId, formData, config)
}

export function goodsAttchUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/goods/file/upload?type=' + type + '&relId=' + relId, formData, config)
}


export function kyCoachOrderAttchUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/ky/coach/order/file/upload?type=' + type + '&relId=' + relId, formData, config)
}


export function courseAttchUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/course/details/file/upload?type=' + type + '&relId=' + relId, formData, config)
}


export function courseVideoUpload(formData, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/course/video/file/upload?relId=' + relId, formData, config)
}

export function courseImgUpload(formData, type, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/course/img/upload?type=img&relId=' + relId, formData, config)
}

export function goodsImgUpload(formData, relId) {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data',
      'X-Token': getAuthToken(),
      'X-Access-Token': getAuthToken()
    }
  }
  relId = relId || ''
  return axios.post(process.env.VUE_APP_BASE_API + '/goods/img/upload?type=img&relId=', formData, config)
}

export function fileUploadCheck(key) {
  return request({
    url: '/course/video/file/check?key=' + key,
    method: 'get'
  })
}
